package com.eunion.web.cms.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eunion.core.PageData;
import com.eunion.web.cms.entity.RechargeRecord;
import com.eunion.web.cms.service.RechargeRecordService;
import com.eunion.web.common.base.BaseService;
import com.eunion.web.common.base.BaseTableCommonController;

/**
 * 充值记录
 */
@Controller
@RequestMapping("/api/recharge")
public class RechargeRecordResource extends BaseTableCommonController<RechargeRecord, Long> {

    @Override
    protected BaseService<RechargeRecord, Long> getBaseService() {
        return rechargeRecordService;
    }

    @Autowired
    private RechargeRecordService rechargeRecordService;

    /**
     * 进入充值记录页面
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return getUrlBase();
    }

    @Override
    protected String getUrlBase() {
        return "admin/recharge/recharge";
    }

    /**
     * 根据会员手机号查询充值分页列表。
     * @param params
     * @return
     */
    @PostMapping(value = "/getByPhone.json", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public PageData<RechargeRecord> getByPhone(@RequestBody PageData<RechargeRecord> params) {
        if (null == params) {
            params = new PageData<RechargeRecord>();
            return params;
        }
        // 分页参数合法性校验
        params.calculate();
        Integer size = params.getPageSize();
        if (null == size || size < PageData.PAGE_SIZE_DEFAULT) {
            size = PageData.PAGE_SIZE_DEFAULT;
        }

        // PageRequest页面从0开始
        int pageNumber = params.getPageNumber() - 1;
        Pageable pageAble = new PageRequest(pageNumber, size);
        Page<RechargeRecord> page = getBaseService().findAll(getCondition(params), pageAble);
        params.setTotal(page.getTotalElements());
        params.setRows(page.getContent());
        params.setRowData(getRowDataJson(page.getContent()));
        // 设置总页数
        params.setPageCount(page.getTotalPages());
        return params;
    }
}
